package br.com.lazyprogrammer.app.ffo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import br.com.lazyprogrammer.app.ffo.R;
import br.com.lazyprogrammer.app.ffo.model.MarketListItem;
import br.com.lazyprogrammer.app.ffo.model.MarketList;

/**
 * Created by Andre on 28/08/2014.
 */
public class MarketListExpandableAdapter extends BaseExpandableListAdapter {

    public interface OnMarketItemSelectedListener {
        public void onItemSelected(MarketListItem item);
    }

    private Context context;

    private List<MarketList> list;

    private OnMarketItemSelectedListener listener;

    public MarketListExpandableAdapter(Context context, List<MarketList> list, OnMarketItemSelectedListener listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getItems().size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return list.get(groupPosition).getLabel();
    }

    @Override
    public MarketListItem getChild(int groupPosition, int childPosititon) {
        return list.get(groupPosition).getItems().get(childPosititon);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.header_market_list, null);
            holder.header = (TextView) convertView.findViewById(R.id.label);
            convertView.setTag(holder);
        }
        else
            holder = (GroupHolder) convertView.getTag();

        holder.header.setText(getGroup(groupPosition));

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_market_list, null);
            holder.label = (TextView) convertView.findViewById(R.id.label);
            convertView.setTag(holder);
        }
        else
            holder = (ChildHolder) convertView.getTag();
        holder.label.setText(getChild(groupPosition, childPosition).getLabel());
        holder.quantity.setText(getChild(groupPosition, childPosition).getQuantity().toString());

        convertView.setClickable(true);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemSelected(getChild(groupPosition, childPosition));
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupHolder {
        TextView header;
    }

    class ChildHolder {
        TextView label;
        TextView quantity;
    }
}
